﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace socketTest1.Client
{
    public partial class Login : Form
    {
        private static Byte[] result = new Byte[1024];
        private Byte[] MsgBuffer;
       private Socket clientSocket =null;

        public Login()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim() == "")
            {
                txtUserName.Focus();
                MessageBox.Show("用户名不能为空", "登陆提示");
                return;
            }
            if (txtPassword.Text.Trim() == "")
            {
                txtPassword.Focus();
                MessageBox.Show("密码不能为空", "登陆提示");
                return;
            }

            UserLogin(txtUserName.Text.Trim(), txtPassword.Text.Trim());
        }

        private void UserLogin(string userName, string password)
        {
            //设定服务器IP地址  
            IPAddress ip = IPAddress.Parse(ServerConfig.server_host);

            clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            SocketError errorCode = SocketError.NoData;

            try
            {
                clientSocket.Connect(new IPEndPoint(ip, ServerConfig.server_port)); //配置服务器IP与端口  

                txtMsg.Text = "连接服务器成功";

                string loginText = "L" + String.Format("{0:D4}", userName.Length) + userName + password;

                result = Encoding.Unicode.GetBytes(loginText);

                clientSocket.Send(result);

                IAsyncResult iar = clientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallBack), null);
            }
            catch (Exception ex)
            {
                txtMsg.Text = "连接服务器失败，请按回车键退出！" + ex.Message + "errorCode:" + errorCode.ToString();
                return;
            }
        }

        private void ReceiveCallBack(IAsyncResult AR)
        {
            try
            {
                //结束挂起的异步读取，返回接收到的字节数。 AR，它存储此异步操作的状态信息以及所有用户定义数据
                int REnd = clientSocket.EndReceive(AR);

                //lock (this.SendMsg)
                //{
                //    this.SendMsg.AppendText(Encoding.Unicode.GetString(MsgBuffer, 0, REnd));
                //}

                clientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null);

            }
            catch
            {
                MessageBox.Show("已经与服务器断开连接！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            RegUser ru = new RegUser();
            ru.ShowDialog(this);
        }
    }
}
